<template>
    <div class="pagination_container">
        <el-pagination v-if="showPagination" v-model:current-page="page" v-model:page-size="props.limit" background
            :total="props.total" @current-change="changePage" />
        <div class="min_pagination" v-else>
            <span class="icons" @click="handlePrev">
                <el-icon size="16" class="icon">
                    <ArrowLeft />
                </el-icon>
            </span>
            <span class="active">{{ page }}</span>
            <span class="icons" @click="handleNext">
                <el-icon size="16" class="icon">
                    <ArrowRight />
                </el-icon>
            </span>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'
import $bus from '@/bus/index.ts'
import useProductStore from '@/store/modules/product.ts'
import useNewStore from '@/store/modules/new.ts'
import { useRoute } from 'vue-router'

const route = useRoute()
const productStore = useProductStore()
const newStore = useNewStore()
const props = defineProps(['page', 'limit', 'total', 'tmId', 'sId'])

let page = ref(props.page)

let showPagination = ref(true) // 是否显示Pc端分页器

onMounted(() => {
    // 页面加载时判断是否是手机端
    if (document.documentElement.clientWidth < 768) {
        showPagination.value = false
    } else {
        showPagination.value = true
    }
    // 根据页面窗口大小改变分页器的状态
    $bus.on('changeShowPagination', (flag: boolean) => {
        showPagination.value = flag
    })
    // 切换导航时清空page数据
    $bus.on('clearPage', (num: number) => {
        page.value = num
    })
    $bus.on('changePagination', (num: number) => {
        page.value = num
    })
})

onUnmounted(() => {
    $bus.off(['changeShowPagination', 'clearPage', 'changePagination']) // 销毁全局事件
})

// 选中页发生变化时触发
const changePage = async (val: number) => {
    if (route.path == '/product') {
        // 产品中心的分页器
        getProducts(val)
    } else if (route.path == '/new') {
        // 新闻动态的分页器
        getNews(val)
    }

}
// 点击上一页时触发
const handlePrev = async () => {
    if (page.value > 1) {
        page.value--
        if (route.path == '/product') {
            // 产品中心的分页器
            getProducts(page.value)
        } else if (route.path == '/new') {
            // 新闻动态的分页器
            getNews(page.value)
        }
    }
}
// 点击下一页时触发
const handleNext = async () => {
    if (route.path == '/product') {
        if (page.value < productStore.pages) {
            page.value++
            // 产品中心的分页器
            getProducts(page.value)
        }
    } else if (route.path == '/new') {
        if (page.value < newStore.pages) {
            page.value++
            // 新闻动态的分页器
            getNews(page.value)
        }
    }
}
// 获取产品分页数据
const getProducts = async (page: number) => {
    productStore.limit = props.limit
    productStore.page = page
    productStore.tmId = props.tmId
    await productStore.getProducts()
}
// 获取新闻动态分页数据
const getNews = async (page: number) => {
    newStore.page = page
    newStore.limit = props.limit
    newStore.sId = props.sId
    await newStore.getNews()
}
</script>

<style lang="scss">
.pagination_container {
    display: flex;
    justify-content: center;
    margin-bottom: 80px;

    .number {
        color: #333;

        &:hover {
            color: #4F68AC;
        }
    }

    .is-active {
        background-color: #4F68AC !important;
    }

    .min_pagination {
        display: flex;
        align-items: center;

        .icons {
            display: inline-block;
            background-color: #F0F2F5;
            width: 36px;
            height: 28px;
            line-height: 28px;
            border-radius: 3px;
            text-align: center;
            cursor: pointer;

            &:hover {
                color: #4F68AC;
            }

            .icon {
                margin-top: 6px;
            }
        }

        .active {
            display: inline-block;
            margin: 0 16px;
            width: 36px;
            height: 28px;
            line-height: 28px;
            border-radius: 3px;
            text-align: center;
            cursor: pointer;
            background-color: #4F68AC;
            color: #fff;
        }
    }
}
</style>